package com.future.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.future.common.global.PlatFormContext;
import com.future.dao.BookedDao;
import com.future.dao.ClassRoomDao;
import com.future.model.Booked;
import com.future.model.Classroom;
import com.future.requestbean.FreeClassRoomQueryBean;
import com.jfinal.aop.Inject;

public class BookedService {
	@Inject
	private BookedDao bookedDao;
	@Inject
	private ClassRoomDao classRoomDao;

	public Map<String, List<Booked>> getAllClassRoomInfo() {
		List<Booked> bookedList = this.bookedDao.findAll();
		List<Classroom> allClassRoom = this.classRoomDao.findAll();
		Map<String, List<Booked>> result = new TreeMap<String, List<Booked>>();
		//已经预定的教室
		bookedList.forEach(booked -> {
			if(booked.getEnddate().compareTo(new Date()) >0){
				if (result.containsKey(booked.getClassroomno())) {
					result.get(booked.getClassroomno()).add(booked);
				} else {
					List<Booked> templist = new ArrayList<Booked>();
					templist.add(booked);
					result.put(booked.getClassroomno(), templist);
				}
			}
		});

		//处理没有预定的教室
		allClassRoom.forEach(classroom -> {
			if(!result.containsKey(classroom.getClassroomno())){
				result.put(classroom.getClassroomno(), new ArrayList<Booked>());
			}
		});
		return result;
	}

	public Map<String, List<Booked>> getMyBookedClassRoom() {
		List<Booked> bookedList = this.bookedDao.getMyBookedClassRoom(PlatFormContext.getCurrentUser().getUserid());
		TreeMap<String, List<Booked>> result = new TreeMap<String, List<Booked>>();

		bookedList.forEach(booked -> {
			if (result.containsKey(booked.getClassroomno())) {
				result.get(booked.getClassroomno()).add(booked);
			} else {
				List<Booked> templist = new ArrayList<Booked>();
				templist.add(booked);
				result.put(booked.getClassroomno(), templist);
			}
		});
		return result;
	}

	public List<Classroom> getFreeClassRoom(FreeClassRoomQueryBean freeClassRoomQueryBean) {
		return this.classRoomDao.getFreeClassRoom(freeClassRoomQueryBean);
	}

}
